from django.db.models import signals

class ArchiveYear:
	"""
	A year in the blog archive.
	
	Note that year.months is a list of datetime objects.
	"""
	def __init__(self, number):
		self.number = number
		self.months = []
	
	def __eq__(self, other):
		return self.number == other.number
	
	def __ne__(self, other):
		return not self.__eq__(other)
	
	def __unicode__(self):
		return str(self.number)

class BlogArchive:
	years = []
	
	def update(self, sender, **kwargs):
		"""
		Method to update the archive with new posts (called from post.save()).
		"""
		self.years = []
		months = sender.public_objects.dates("published", "month")
		for date in months:
			year = ArchiveYear(date.year)
			if year not in self.years:
				self.years.append(year)
				year.months.append(date)
			else:
				for y in self.years:
					if y.number == year.number:
						y.months.append(date)
						continue
		return None

archive = BlogArchive()
